﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using Windows.Storage.Streams;
using Windows.Data.Json;

namespace MyHelloWinJson
{

    public class JsonData
    {
        
        public string Sign { get; set; }
       
        public string DeviceID { get; set; }
        
        public string DeviceKey { get; set; }
      
        public string AuthKey { get; set; }

        public bool CanUnLock { get; set; }
    }

    public class CMyHelloWinJson
    {

        public static JsonData ParesToData(string js)
        {
            JsonData rv = null;
            try
            {
                JsonObject schoolObject = JsonObject.Parse(js);
                if (schoolObject != null)
                {
                    rv = new JsonData();
                    rv.Sign = schoolObject.GetNamedString("Sign");
                    rv.DeviceID = schoolObject.GetNamedString("DeviceID");
                    rv.DeviceKey = schoolObject.GetNamedString("DeviceKey");
                    rv.AuthKey = schoolObject.GetNamedString("AuthKey");
                    rv.CanUnLock = schoolObject.GetNamedBoolean("CanUnLock");
                }
            }catch(Exception e)
            {
                return rv;
            }
 
            return rv;
        }

        public static string ParesToJson(JsonData Dat)
        {
            JsonObject schoolObject = new JsonObject();
            schoolObject.SetNamedValue("Sign", JsonValue.CreateStringValue(Dat.Sign));
            schoolObject.SetNamedValue("DeviceID", JsonValue.CreateStringValue(Dat.DeviceID));
            schoolObject.SetNamedValue("DeviceKey", JsonValue.CreateStringValue(Dat.DeviceKey));
            schoolObject.SetNamedValue("AuthKey", JsonValue.CreateStringValue(Dat.AuthKey));
            schoolObject.SetNamedValue("CanUnLock", JsonValue.CreateBooleanValue(Dat.CanUnLock));
            return schoolObject.ToString();
        }
    }
}
